from django.http import HttpResponse, JsonResponse
from django.views.decorators.http import require_http_methods
import base64, json
from .confs.conf import get_mirror_conn
from django.views.decorators.csrf import csrf_exempt

def_response_o = {
    "code": 0,
    "result": "",
    "message": "",
    "type": 'success',
  }


insert = '''INSERT INTO `dlz_after_sales_feedback`(`main_id`, `event_name`, `event_value`, `content`, `redundancy_1`, `redundancy_2`, `redundancy_3`) VALUES (%s, %s, %s, %s, %s, %s, %s)'''

def get_data(params:[]):
    result=[]
    if params:
        for x in params:
            v={
                'main_id':x["main_id"] if x["main_id"] else '',
                'event_name': x["event_name"] if x["event_name"] else '',
                'event_value': x["event_value"] if x["event_value"] else '',
                'content': x["content"] if "content" in x else "",
                'redundancy_1': x["redundancy_1"] if "redundancy_1" in x else "",
                'redundancy_2': x["redundancy_2"] if "redundancy_2" in x else "",
                'redundancy_3': x["redundancy_3"] if "redundancy_3" in x else ""
            }
            result.append(v)
    return result

@require_http_methods(["POST"])
@csrf_exempt
def save_dlz_feedback(request):
    print("哈哈哈哈")
    global insert
    params = json.loads(request.body)
    print(params)
    data=get_data(params)
    db_conn = get_mirror_conn()
    if data:
        for x in data:
            db_conn.insert('dlz_after_sales_feedback', x)
    re = json.loads(json.dumps(def_response_o,ensure_ascii=False))
    return JsonResponse(re)